/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.tang.common.config;

import com.tang.modules.sys.shiro.ShiroTag;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * <p>
 * Freemarker配置，Freemarker作为视图模板
 * FreeMarker是一款模板引擎，即一种基于模板和要改变的数据，
 * 并用来生成输出文本（HTML网页、电子邮件、配置文件、源代码等）的通用工具
 * 它不是面向最终用户的，而是一个Java类库，是一款程序员可以嵌入他们所开发产品的组件
 * </p>
 * @author Mark 15062230055@163.com
 * @since 3.0.0 201809-28
 */
@Configuration
public class FreemarkerConfig {

    /**
     * 下面的在xml的配置形式为（参考https://blog.csdn.net/songhaifengshuaige/article/details/54136984）：
     * <bean id="freemarkerConfiguration"
     *      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
     *     <property name="location" value="classpath:freemarker.properties"/>
     * </bean>
     * <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
     *       <property name="freemarkerSettings" ref="freemarkerConfiguration"/>
     *       <property name="templateLoaderPath" value="/WEB-INF/" />
     *       <property name="defaultEncoding" value="UTF-8" />
     *       <property name="freemarkerVariables">
     *                <map>
     *                   <entry key="shiro" value-ref="shiroTag" />
     *               </map>
     *       </property>
     * </bean>
     * <bean id="shiroTag" class="freemarker.template.utility.ShiroTag" />
     * @param shiroTag
     * @return
     */
    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer(ShiroTag shiroTag){
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPath("classpath:/templates");
        Map<String, Object> variables = new HashMap<>(1);
        variables.put("shiro", shiroTag);
        configurer.setFreemarkerVariables(variables);

        Properties settings = new Properties();
        settings.setProperty("default_encoding", "utf-8");
//        settings.setProperty("output_encoding", "utf-8");
//        settings.setProperty("locale", "zh_CN");
        settings.setProperty("number_format", "0.##");
//        settings.setProperty("time_format", "HH:mm:ss");
//        settings.setProperty("date_format", "yyyy-MM-dd");
//        settings.setProperty("datetime_format", "yyyy-MM-dd HH:mm:ss");
        configurer.setFreemarkerSettings(settings);
        return configurer;
    }

}
